'use strict';

const Controller = require('egg').Controller;
const {
	REFUSED
} = require('dns');
const fs = require("fs")
const svgCaptcha = require('svg-captcha');

class HomeController extends Controller {
	// 退出登录
	async outlogin(){
		this.ctx.session.username=null
		this.ctx.body={code:2001}
	}
	async outlogin() {
		this.ctx.session.username = null
		this.ctx.body = { code: 2001 }
	}

	async getpinlun() {
		let sql = `SELECT * FROM pinlunimg`
		let res = await this.app.mysql.query(sql)
		this.ctx.body = {
			info: res,
			code: 3002
		}
	}


	async pinlundata() {
		let sql = `SELECT * FROM pinlun`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = {
			info: res,
			code: 3001
		}
	}
	//广场评论
	async share() {
		let ziduan = this.ctx.request.body
		console.log(ziduan)
		let phone = this.ctx.session.username
		let sql = `INSERT INTO pinlun (phone,pinlun) VALUES ("${phone}","${ziduan.textarea}")`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = {
			info: "评论成功"
		}
	}
	// 获取个人信息
	async getuserinfo() {
		let phone = this.ctx.session.username
		if (!phone) {
			this.ctx.body = { code: 4004 }
		}  else {
			let sql = `SELECT touxiang,email FROM user WHERE phone="${phone}"`
			let re = await this.app.mysql.query(sql)
			this.ctx.body = {
				code: 2004,
				...re[0]
			}
		}

	}
	// keyword关键字搜索
	async keyword() {
		let ziduan = this.ctx.request.query
		let sql = `SELECT * FROM xwgoods WHERE title like "%${ziduan.keyWord}%"`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res[0]
	}
	// 模糊搜索，所有数据
	async alldata() {
		let sql = `SELECT * FROM xwgoods`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	// 写评论，并存入数据库中
	async publish() {
		let ziduan = this.ctx.request.query
		console.log(ziduan)
		let phone = this.ctx.session.username
		let sql = `INSERT INTO opinion (gid,phone,opinion) VALUES ("${ziduan.gid}","${phone}","${ziduan.opinion}")`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = { info: "发表成功" }
		this.ctx.body = {
			info: "发表成功"
		}
	}
	// 获取评论内容
	async getopinion(){
		let ziduan=this.ctx.request.query
   let phone=this.ctx.session.username 
     let sql=`SELECT * FROM opinion INNER JOIN user WHERE user.phone=opinion.phone AND gid=${ziduan.gid}`
	 let res=await this.ctx.app.mysql.query(sql)
	 this.ctx.body=res
	}
  async xqydata(){
    let ziduan=this.ctx.request.query
    console.log(ziduan,11111111)
    let sql=`SELECT * FROM xwgoods where gid=${ziduan.id}`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res[0]
    console.log(res)
  }
  async index() {
    const { ctx } = this;
    ctx.body = 'hello';
  }
  
  async sign() {
    // var files=this.ctx.request.files  //关闭csrf 开启文件模式
	var ziduan =this.ctx.request.body
	console.log(ziduan)
	this.ctx.body={info:"ok",code:200}
  }
  async getmovie(){
    const {ctx}=this;
    ctx.body='111'
  }
  async bzlx(){
    let sql=`SELECT * FROM xwgoods11 LIMIT 0,8`
	console.log(1111111)
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }
  async sszk(){
    let sql=`SELECT * FROM xwgoods11 LIMIT 8,8`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }
  async addgoods11(){
    let f=this.ctx.request.files
    let ziduan=this.ctx.request.body
    console.log(ziduan,f,11111)
    let oldpath=f[0].filepath
    let newpath=__dirname+"/../public/xw/img/"+path.basename(oldpath)
    fs.renameSync(oldpath,newpath)
    ziduan[f[0].field]="/img/"+path.basename(oldpath)
		let sql=`insert into xwgoods11 (title,name,img) values ("${ziduan.title}","${ziduan.name}","${ziduan.img}")`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body={code:200}
  }
  async addgoods(){
    let f=this.ctx.request.files
    let ziduan=this.ctx.request.body
    console.log(ziduan,f,11111)
    let oldpath=f[0].filepath
    let newpath=__dirname+"/../public/xw/img/"+path.basename(oldpath)
    fs.renameSync(oldpath,newpath)
    ziduan[f[0].field]="/img/"+path.basename(oldpath)
		let sql=`insert into xwgoods (title,name,img) values ("${ziduan.title}","${ziduan.name}","${ziduan.img}")`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body={code:200}
  }
  async zuire(){
    let sql=`SELECT * FROM xwgoods LIMIT 0,8`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }
  async huayu(){
    let sql=`SELECT * FROM xwgoods LIMIT 8,8`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }
  async oumei(){
    let sql=`SELECT * FROM xwgoods LIMIT 16,8`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }
  async rihan(){
    let sql=`SELECT * FROM xwgoods LIMIT 24,8`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }
  async danqu(){
    let sql=`SELECT * FROM xwgoods LIMIT 32,8`
    let res=await this.ctx.app.mysql.query(sql)
    this.ctx.body=res
  }

	async getopinion() {
			let ziduan = this.ctx.request.query
			let phone = this.ctx.session.username
			let sql = `SELECT * FROM opinion INNER JOIN user WHERE user.phone=opinion.phone AND gid=${ziduan.gid}`
			let res = await this.ctx.app.mysql.query(sql)
			this.ctx.body = res
		} 
	// 	let ziduan = this.ctx.request.query
	// 	let phone = this.ctx.session.username
	// 	let sql = `SELECT * FROM opinion INNER JOIN user WHERE user.phone=opinion.phone AND gid=${ziduan.gid}`
	// 	let res = await this.ctx.app.mysql.query(sql)
	// 	this.ctx.body = res
	// }

	async xqydata() {
		let ziduan = this.ctx.request.query
		console.log(ziduan, 11111111)
		let sql = `SELECT * FROM xwgoods where gid=${ziduan.id}`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res[0]
		console.log(res)
	}
	async index() {
		const { ctx } = this;
		ctx.body = 'hello';
	}
	async sign() {
		// var files=this.ctx.request.files  //关闭csrf 开启文件模式
		var ziduan = this.ctx.request.body
		console.log(ziduan)
		this.ctx.body = { info: "ok", code: 200 }
	}
	async getmovie() {
		const { ctx } = this;
		ctx.body = '111'
	}
	async bzlx() {
		let sql = `SELECT * FROM xwgoods11 LIMIT 0,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	async sszk() {
		let sql = `SELECT * FROM xwgoods11 LIMIT 8,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	async addgoods11() {
		let f = this.ctx.request.files
		let ziduan = this.ctx.request.body
		console.log(ziduan, f, 11111)
		let oldpath = f[0].filepath
		let newpath = __dirname + "/../public/xw/img/" + path.basename(oldpath)
		fs.renameSync(oldpath, newpath)
		ziduan[f[0].field] = "/img/" + path.basename(oldpath)
		let sql = `insert into xwgoods11 (title,name,img) values ("${ziduan.title}","${ziduan.name}","${ziduan.img}")`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = { code: 200 }
	}
	async addgoods() {
		let f = this.ctx.request.files
		let ziduan = this.ctx.request.body
		console.log(ziduan, f, 11111)
		let oldpath = f[0].filepath
		let newpath = __dirname + "/../public/xw/img/" + path.basename(oldpath)
		fs.renameSync(oldpath, newpath)
		ziduan[f[0].field] = "/img/" + path.basename(oldpath)
		let sql = `insert into xwgoods (title,name,img) values ("${ziduan.title}","${ziduan.name}","${ziduan.img}")`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = { code: 200 }
	}
	async zuire() {
		let sql = `SELECT * FROM xwgoods LIMIT 0,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	async huayu() {
		let sql = `SELECT * FROM xwgoods LIMIT 8,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	async oumei() {
		let sql = `SELECT * FROM xwgoods LIMIT 16,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	async rihan() {
		let sql = `SELECT * FROM xwgoods LIMIT 24,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}
	async danqu() {
		let sql = `SELECT * FROM xwgoods LIMIT 32,8`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
	}

	async sign() {
	// var files=this.ctx.request.files  //关闭csrf 开启文件模式
	var ziduan = this.ctx.request.body
	console.log(ziduan)
	this.ctx.body = {
		info: "ok",
		code: 200
	}
}
	async getmovie() {
	const {
		ctx
	} = this;
	ctx.body = '111'
}
	async bzlx() {
	let sql = `SELECT * FROM xwgoods11 LIMIT 0,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}
	async sszk() {
	let sql = `SELECT * FROM xwgoods11 LIMIT 8,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}
	async addgoods11() {
	let f = this.ctx.request.files
	let ziduan = this.ctx.request.body
	console.log(ziduan, f, 11111)
	let oldpath = f[0].filepath
	let newpath = __dirname + "/../public/xw/img/" + path.basename(oldpath)
	fs.renameSync(oldpath, newpath)
	ziduan[f[0].field] = "/img/" + path.basename(oldpath)
	let sql =
		`insert into xwgoods11 (title,name,img) values ("${ziduan.title}","${ziduan.name}","${ziduan.img}")`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = {
		code: 200
	}
}
	async addgoods() {
	let f = this.ctx.request.files
	let ziduan = this.ctx.request.body
	console.log(ziduan, f, 11111)
	let oldpath = f[0].filepath
	let newpath = __dirname + "/../public/xw/img/" + path.basename(oldpath)
	fs.renameSync(oldpath, newpath)
	ziduan[f[0].field] = "/img/" + path.basename(oldpath)
	let sql = `insert into xwgoods (title,name,img) values ("${ziduan.title}","${ziduan.name}","${ziduan.img}")`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = {
		code: 200
	}
}
	async zuire() {
	let sql = `SELECT * FROM xwgoods LIMIT 0,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}
	async huayu() {
	let sql = `SELECT * FROM xwgoods LIMIT 8,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}
	async oumei() {
	let sql = `SELECT * FROM xwgoods LIMIT 16,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}
	async rihan() {
	let sql = `SELECT * FROM xwgoods LIMIT 24,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}
	async danqu() {
	let sql = `SELECT * FROM xwgoods LIMIT 32,8`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
}


	// cll
	async index() {
	const {
		ctx
	} = this;
	ctx.body = 'hello';
}
	async rember() { //记住我
	let ziduan = this.ctx.request.body
	let sql = `SELECT * FROM user WHERE token="${ziduan.token}"`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
	console.log(ziduan, 3333333333333)
	console.log(res[0], 4444444444)

}
	async register() { //注册
	// var files=this.ctx.request.files  
	let ziduan = this.ctx.request.body
	console.log(ziduan)
	// this.ctx.body={info:"ok",code:2001}
	// 把账号信息写入数据库
	//【1】查询邮箱是否注册过
	let sql1 = `SELECT * FROM user WHERE phone="${ziduan.phone}" OR email="${ziduan.email}"`
	let re1 = await this.app.mysql.query(sql1)
	if (re1[0]) {
		this.ctx.body = {
			info: "注册失败，该账号已存在！",
			code: 4001
		};
		console.log(re1, 1111111)
	} else {
		//【2】插入数据
		let ctime = new Date().toLocaleDateString()
		let touxiang = "/cll/default_touxiang.jpg" //默认头像
		// 使用token把账号加密为暗文---JSON Web Token技术（jwt）
		const token = this.app.jwt.sign({
			id: ziduan.phone.id //根据id加密用户信息
		}, this.app.config.jwt.secret);
		let sql2 =
			`INSERT INTO user (phone,email,pwd,touxiang,ctime,token) VALUES ("${ziduan.phone}","${ziduan.email}","${ziduan.pwd}","${touxiang}","${ctime}","${token}")`
		let result = await this.app.mysql.query(sql2)
		console.log(token, 66666666666);
		console.log(result, 77777777);
		this.ctx.body = {
			info: "注册账号成功，欢迎加入WeKnow，请登录!",
			code: 2001
		};
		console.log(result, 2222222222)
	}
}
	async verify() { //验证码
	//1.生成验证码
	let captcha = svgCaptcha.create({
		width: 160,
		height: 56,

		size: 4,
		color: true,
		background: "#eee",
		noise: 4
	});
	//2设置缓存
	this.ctx.session.verif = captcha.text
	//3发给前端
	this.ctx.body = captcha.data
}
	async login() { //登录
	let ziduan = this.ctx.request.body //获取用户输入
	console.log(ziduan)
	//[1]验证验证码是否正确
	let verif = this.ctx.session.verif
	if (ziduan.verif != verif) {
		this.ctx.body = {
			info: "登录失败，验证码错误！",
			code: 4004
		}
	}
	//[2]验证邮箱是否存在
	let sql1 = `SELECT * FROM user WHERE phone="${ziduan.username}" OR email="${ziduan.username}"`
	let re1 = await this.app.mysql.query(sql1)
	if (!re1[0]) { //不存在
		this.ctx.body = {
			info: "登录失败，用户不存在！",
			code: 4002
		}
	} else { //存在
		//[3]验证密码是否正确是否跟邮箱匹配
		let sql2 =
			`SELECT * FROM user WHERE  phone="${ziduan.username}" AND pwd="${ziduan.pwd}"`
		let isMatch = await this.ctx.app.mysql.query(sql2)
		console.log(isMatch[0], 999999999);
		if (isMatch[0]) {
			//设置缓存
			// this.ctx.cookies.set("username",ziduan.username,{maxAge:1000*60*60*24*365})
			this.ctx.session.username = ziduan.username
			// // 使用token把账号加密为暗文---JSON Web Token技术（jwt）
			// const token = this.app.jwt.sign({
			// 	id: isMatch[0].id //根据id加密用户信息
			// }, this.app.config.jwt.secret);

			// let sql3 =
			// 	`UPDATE user SET token="${token}" WHERE phone="${ziduan.username}" AND pwd="${ziduan.pwd}"`
			// let result = await this.app.mysql.query(sql3)

			// console.log(result, 1111111);
			let token = isMatch[0].token
			this.ctx.body = {
				info: "登录成功，欢迎来到WeKnow！",
				code: 2002,
				token
			}
		} else {
			this.ctx.body = {
				info: "登录失败，密码错误！",
				code: 4003
			}
		}
	}
}
	async forget() { //重置密码
	let ziduan = this.ctx.request.body //获取用户输入
	console.log(ziduan)
	let sql3 = `SELECT * FROM user WHERE phone="${ziduan.phone}" AND email="${ziduan.email}"`
	let re3 = await this.app.mysql.query(sql3)
	if (!re3[0]) { //不存在
		this.ctx.body = {
			info: "重置密码失败，用户不存在！",
			code: 4005
		}
	} else { //存在
		let sql4 =
			`UPDATE user SET pwd="${ziduan.pwd}" WHERE phone="${ziduan.phone}" AND email="${ziduan.email}"`
		let resetpwd = await this.ctx.app.mysql.query(sql4)
		this.ctx.body = {
			info: "重置密码成功！",
			code: 2003
		}
	}
}
	async jiema() {
	let ziduan = this.ctx.request.body //把发送的token解密为保存的数据
	console.log(ziduan);
	let re = this.ctx.app.jwt.verify(ziduan.token, this.app.config.jwt.secret)
	console.log(re, 2222222222222222);
	this.ctx.body = {
		info: "解码"
	}
}

	/* dj */
	// 添加电影详细数据
	async add() {
	const {
		ctx
	} = this;
	console.log(ctx.request.body)
	let {
		mname,
		score,
		time,
		desc,
		dire
	} = ctx.request.body
	const data = await ctx.service.movie.add({
		mname,
		score,
		time,
		desc,
		dire
	});
	console.log(data);
}

	// 电影页导航栏搜索
	async search() {
	const {
		ctx
	} = this;
	const data = await ctx.service.movie.search(ctx.request.query[0]);
	console.log(data);
	// 如果搜索到了输入内容相关的东西，则返回code：1，msg：data
	// 如果没有搜索到东西则返回code：0
	if (data.length == 0) {
		ctx.body = {
			code: 0,
			msg: '未搜索到相关信息'
		}
	} else {
		ctx.body = {
			code: 1,
			msg: data
		}
	}
	// ctx.body='111';
}
	// 电影页电影item
	async getmovie() {
	const {
		ctx
	} = this;
	const recentPlaylist = await ctx.service.movie.getmovie();
	ctx.body = recentPlaylist;
}

	//吴宜航轮播

	async wyhlunbo() {
	let sql = `SELECT * FROM wyhimg`
	let res = await this.ctx.app.mysql.query(sql)
	this.ctx.body = res
	console.log("22222");
	}
	// 同城页面
	async hotact() {
		let sql = `SELECT * FROM hotactivity LIMIT 25`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
		console.log(res, 1111111);
	};
	async drama() {
		let sql = `SELECT * FROM drama LIMIT 25`
		let res = await this.ctx.app.mysql.query(sql)
		this.ctx.body = res
		console.log(res, "bbbbbbb");
	};

}

module.exports = HomeController;